package com.example.demo.thread;

import java.util.HashMap;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;


/**
 * @author czj
 *
 * Callable 方法有回调方法 可以允许有返回值
 */
public class CallableTest implements Callable<Integer> {

    private String name;

    private static Integer num = 0;

    public CallableTest(String name){
        this.name = name;
    }

    @Override
    public Integer call() throws Exception {
        Thread.currentThread().setName(name);
        ++ num;
        int seconds = num * 1000;
        System.out.println(Thread.currentThread().getName()+",sleep "+num+" seconds");
        Thread.sleep(seconds);
        return seconds;
    }

    public static void main(String[] args) {
        HashMap<Object, Object> map = new HashMap(){
            {
                put("sss","sss");
                put("sss","sss");
                put("sss","sss");
                put("sss","sss");
                put("sss","sss");
                put("sss","sss");
                put("sss","sss");
            }
        };

        for (int i = 0; i < 5; i++) {
            CallableTest callableTest = new CallableTest(i + "");
            FutureTask task = new FutureTask(callableTest);
            try {
                new Thread(task).start();
                System.out.println(task.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }
    }
}
